package com.otitan.la.forest.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.la.forest.system.dto.AreaHistoryDTO;
import com.otitan.la.forest.system.entity.AreaHistoryEntity;
import com.otitan.la.forest.system.mapper.AreaHistoryMapper;
import com.otitan.la.forest.system.service.AreaHistoryService;
import com.otitan.la.forest.system.vo.AreaHistoryVO;
import com.otitan.webapp.framework.basepro.exception.code.BaseResponseCode;
import com.otitan.webapp.framework.basepro.model.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;

@Slf4j
@Service
public class AreaHistoryServiceImpl extends ServiceImpl<AreaHistoryMapper, AreaHistoryEntity> implements AreaHistoryService {

    @Override
    public DataResult add(AreaHistoryEntity areaHistory) {
        boolean isSuccess = this.save(areaHistory);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult batchDelete(List<String> ids) {
        boolean isSuccess = this.removeByIds(ids);
        log.debug("\n>>>>>>>>> 【删除行政区划表】 <<<<<<<<<\n操作结果：{}，ids：{}\n>>>>>>>>> ---- <<<<<<<<<", isSuccess, ids);
        return DataResult.success();
    }

    @Override
    public DataResult editor(AreaHistoryEntity areaHistory, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        boolean isSuccess = this.updateById(areaHistory);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult findPageData(long page, long size, AreaHistoryDTO areaHistory) {
        Page<AreaHistoryVO> pagePlug = new Page<>(page, size);
        pagePlug.setRecords(this.baseMapper.findPageData(areaHistory, pagePlug));
        return DataResult.success(pagePlug);
    }

    @Override
    public DataResult findOne(String id) {
        return DataResult.success(this.getById(id));
    }
}